from tortoise import fields
from tortoise.models import Model

class User(Model):
    id = fields.IntField(pk=True)  # 主键，自增
    username = fields.CharField(max_length=50, unique=True)  # 用户名，唯一
    email = fields.CharField(max_length=100, unique=True)  # 邮箱，唯一
    is_active = fields.BooleanField(default=True)  # 是否激活
    created_at = fields.DatetimeField(auto_now_add=True)  # 创建时间
    updated_at = fields.DatetimeField(auto_now=True)  # 更新时间
    role_id = fields.IntField(null=True)

    def __str__(self):
        return self.username

class Role(Model):
    id = fields.IntField(pk=True)  # 主键，自增
    name = fields.CharField(max_length=50, unique=True)  # 用户名，唯一
    desc = fields.CharField(max_length=200)
    created_at = fields.DatetimeField(auto_now_add=True)  # 创建时间
    updated_at = fields.DatetimeField(auto_now=True)  # 更新时间
    
class Course(Model):
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=255)
    description = fields.TextField(null=True)
    created_at = fields.DatetimeField(auto_now_add=True)
    updated_at = fields.DatetimeField(auto_now=True)

    users = fields.ManyToManyField("models.User", related_name="courses", through="usercourse")

    def __str__(self):
        return self.name

class UserCourse(Model):
    user = fields.ForeignKeyField("models.User", related_name="user_courses")
    course = fields.ForeignKeyField("models.Course", related_name="course_users")
    enrolled_at = fields.DatetimeField(auto_now_add=True)

    class Meta:
        table = "usercourse"

class ChatAssistant(Model):
    id = fields.IntField(pk=True)
    role = fields.CharField(max_length=20)
    event = fields.CharField(max_length=100)
    result = fields.CharField(max_length=50)
    history = fields.CharField(max_length=200)
    created_at = fields.DatetimeField(auto_now_add=True)
    updated_at = fields.DatetimeField(auto_now=True)

